/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gride.plugin.error;

import gride.events.EventListener;
import gride.events.GrideEvents;
import gride.plugin.GridePlugin;
import gride.gui.GUIManager;
import gride.project.GrideScript;
import java.awt.Color;

/**
 *
 * @author lukasz
 */
public class ErrorManagerPlugin extends GridePlugin
{
    private ErrorManagerGUIComponent gui;
    private GrideScript lastExecutedFile;


    /**
     *
     */
    public ErrorManagerPlugin()
    {
        gui = new ErrorManagerGUIComponent(this);
        lastExecutedFile = null;
    }


    

    /**
     *
     */
    @Override
    public void activate()
    {
        final Object myself = this;

        guiManager.addComponent(GUIManager.BOTTOM_PANE, gui, "Errors");

        eventManager.addListener(GrideEvents.EXECUTING_FILE, new EventListener()
        {
           public Object whoIsListening()
           {
               return myself;
           }

           public void eventRaised(String eventID, Object[] args)
           {
               lastExecutedFile = (GrideScript) args[0];
           }

        });

        eventManager.addListener(GrideEvents.EXECUTION_EXCEPTION,
                new EventListener()
        {
            public Object whoIsListening()
            {
                return myself;
            }

            public void eventRaised(String eventID, Object[] args)
            {
                // first argument is the exception that terminated execution
                displayErrorInfo(new ErrorDescription(projectManager,
                                                      (Exception)args[0]));
            }
        });

        eventManager.addListener(GrideEvents.COMPILATION_ERROR,
                new EventListener()
        {
            public Object whoIsListening()
            {
                return myself;
            }

            public void eventRaised(String eventID, Object[] args)
            {
                // first argument is the exception that terminated execution
                displayErrorInfo(new ErrorDescription(projectManager,
                                                      (String)args[0]));
            }
        });

        active = true;
    }

    /**
     * 
     */
    @Override
    public void deactivate()
    {
        guiManager.removeComponent(gui);

        eventManager.removeAllListenersFrom(this);

        active = false;
    }

    private void displayErrorInfo(ErrorDescription dsc)
    {
        gui.setErrorDescription(dsc);
        guiManager.focusOnComponent(gui);

        // find the file where the error occured
        String scriptName = dsc.getFilename().replace("/", ".")
                                             .replace(".groovy", "");
        GrideScript script = null;
        for (GrideScript s : projectManager.getCurrentProject().getScripts())
            if (s.getFullName().equals(scriptName))
            {
                script = s;
                break;
            }

        // highlight the line where the error occured
        if (script != null)
        {
            Object[] args = { script, dsc.getLineNumber(), Color.RED };

            eventManager.raiseEvent(GrideEvents.HIGHLIGHT_FILE_LINE, args);
        }
    }

}
